const db = require('../dbconfig')
var url = require('url');
var util = require('util');
const userController = {
    // 获取用户列表
    getUserList: (req,res) => {
        const len = Object.keys(req.body).length
        if(!len){
            const sql = 'select * from user'
            const sqlArr = []
            const callback = (err,data) => {
                if(err) {
                    console.log('请求失败')
                }else{
                    res.send({
                        'data': data,
                        'code': 200,
                        'msg': '查询成功'
                    })
                }
            }
            db.sqlConnect(sql,sqlArr,callback)
        }
    },
    // 用户登录处理
    userLogin: (req,res) => {
        const sql = 'select * from user where username=? and password=?'
        Object.keys(req.body) ? {username,password} = req.body : {username: null,password:null}
        const sqlArr = [username,password]
        const callback = (err,data) => {
            if(err){
                console.log('连接失败')
            }else{
                if(data.length){
                    res.send({
                        'data': data,
                        'code': 200,
                        'TOKEN': Math.random() * 99999999,  // 模拟的token
                        'msg': '登录成功'
                    })
                }else{
                    res.send({
                        'code': 0,
                        'msg': '登录失败'
                    })
                }
            }
        }
        db.sqlConnect(sql,sqlArr,callback)
    },
    // 用户查询
    searchUser: (req,res) => {
        const {username,phone} = req.query
        console.log(req.query)
        let sql,sqlArr;
        if(username !== '' && phone !== ''){
            sql = 'select * from user where username=? and phone=?'
            sqlArr = [username,phone]
        }
        const callback = (err,data) => {
            if(err){
                console.log("查询错误")
            }else{
                res.send({
                    data: data,
                    code: 200,
                    msg: '用户查询成功'
                })
            }
        }
        db.sqlConnect(sql,sqlArr,callback)
    },
    // 用户添加
    addUser: (req,res) => {
        const sqlArr = Object.values(req.body.user)
        console.log(sqlArr)
        const sql = 'insert into user(username,password,email,phone,status) values(?,?,?,?,?)'
        const callback = (err,data) => {
            if(err){
                console.log('用户添加失败',err)
                res.send({
                    msg: '用户添加失败',
                    code: 0
                })
            }else{
                res.send({
                    msg: '添加用户成功',
                    code: 200,
                    data: data
                })
            }
        }
        db.sqlConnect(sql,sqlArr,callback)
    },
    // 删除用户
    delUser: (req,res) => {
        const {key} = req.body[0]
        const sql = 'delete from user where id = ?'
        const sqlArr =  [key]
        const callback = (err,data) => {
            if(err){
                console.log("删除错误")
            }else{  
                res.send({
                    data: data,
                    code: 200,
                    msg: '用户删除成功'
                })
            }
        }
        db.sqlConnect(sql,sqlArr,callback)
    },
    // 更新用户
    updataUser: (req,res) => {
        const sqlArr = Object.values(req.body)
        sqlArr.push(sqlArr[0])
        const colList = Object.keys(req.body)
        let updateStr = ''
        colList.forEach((it,index) => {
            if(index < colList.length - 1){
                updateStr += (it + '=?,')

            }else{
                updateStr += (it + '=?')

            }
        })
        console.log(sqlArr,'跟新用户',updateStr)
        const sql = `update user set ${updateStr} where id=?`
        const callback = (err,data) => {
            if(err){
                console.log(err)
                console.log('用户数据更新失败')
            }else{
                res.send({
                    code: 200,
                    message: '更新成功',
                    data
                })
            }
        }
        db.sqlConnect(sql,sqlArr,callback)
    }
}


module.exports = userController